package com.server;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.database.*;
import com.Helper.*;

public class ChangeFlightServlet extends CustomerServlet
{
	private static final long serialVersionUID = 1L;

	@Override
	//@requires HttpServletRequest !=NULL,  HttpServletResponse !=NULL ;
    //@assignable username,resID;
    //@ensures password=db.GetPassword(), username=db.UserExists(username);
	//@signals_only RuntimeException, IOException,ServletException;
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{
		ConnectDB();
		String username = req.getParameter("username");//get user name
		String resID = req.getParameter("resID");//get reservation ID
				
		try 
		{
			SeatHelper.Setup(connection);
			ReservationHelper.Setup(connection, username);
			
			float refundPrice = ReservationHelper.GetSelectedReservation(Integer.parseInt(resID)).paidPrice;
			int refundPoint = ReservationHelper.GetSelectedReservation(Integer.parseInt(resID)).paidPoint;
			
			CustomerHelper.Setup(connection);
			CustomerHelper.AddPoint(username, refundPoint);
			
			ReservationInfo info = ReservationHelper.GetSelectedReservation(Integer.parseInt(resID));
			SeatHelper.MarkEmpty(info.seat, info.flightID);
			ReservationHelper.CancelReservation(resID);
			
			req.setAttribute("username", username);
			req.getRequestDispatcher("flight/search_flight.jsp").forward(req, resp);
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

